package org.jimthompson.delinkious;

import java.net.MalformedURLException;
import java.util.Map;

import com.redv.bloggerapi.client.Blogger;
import com.redv.bloggerapi.client.BloggerImpl;
import com.redv.bloggerapi.client.Fault;
import com.redv.bloggerapi.client.MetaWeblog;
import com.redv.bloggerapi.client.Post;
import com.redv.bloggerapi.client.User;

public class MetaBlog implements Blog
{
	public MetaBlog(String url, String user, String password, String blog_id) 
	     throws MalformedURLException
	{
		b_url = url;
		b_user = user;
		b_password = password;
		b_blog_id = blog_id;
		
		BloggerImpl blimp = new BloggerImpl(b_url);

		b_blogger = blimp;
		b_meta_weblog = blimp;
	}

	public BlogPost findMostRecentPostInCategory(String target_category, int count)
	{
		
		Integer int_count = new Integer(count);
		Object posts[];
		
		try 
		{
			posts = b_meta_weblog.getRecentPosts(b_blog_id, b_user, b_password, int_count.toString());
			
			// If no category is specified, simply return the most recent post
			if ((target_category == null) ||
				(target_category.compareTo("") == 0))
			{
				Post p = new Post((Map) posts[0]);
                return new MetaBlogPost(p);
			}
			
			for (int i = 0; i < posts.length; i++) {
				
				Map post_map = (Map) posts[i];
				
				Object o[] = (Object[]) post_map.get("categories");
								
				for (int j = 0; j < o.length; j++) {
					String post_category = (String) o[j];

					if (target_category.compareTo(post_category) == 0) {
						//dumpMap(post_map);
						Post p = new Post(post_map);
                        return new MetaBlogPost(p);
					}
				}
			}
		} 
		catch (Fault e) 
		{
			System.out.println("bad mojo: " + e.getCode());
			System.out.println("message: " + e.getMessage());
			System.out.println("cause: " + e.getCause());
			e.printStackTrace();
		}
		
		return null;
	}

	void probe()
	{
		Blogger blogger;
		@SuppressWarnings("unused") MetaWeblog mweblog;
		
		try 
		{
			BloggerImpl blimp = new BloggerImpl(b_url);
			blogger = blimp;
			mweblog = blimp;
			
			User user = blogger.getUserInfo("api_key", b_user, b_password);

//			blogger = new BloggerImpl("http://jimthompson.org/wp/xmlrpc.php");
//			User user = blogger.getUserInfo("dummy", "jct", "passwordhere");
			
			System.out.println("user id = " + user.getUserid());
			System.out.println("email = " + user.getEmail());
			System.out.println("First name = " + user.getFirstname());
			System.out.println("Last name = " + user.getLastname());
			System.out.println("Nickname = " + user.getNickname());
			System.out.println("URl = " + user.getUrl());
			
			com.redv.bloggerapi.client.Blog blogs[] = 
				blogger.getUsersBlogs("api_key", b_user, b_password);
			
			for (int i = 0; i < blogs.length; i++)
			{
				System.out.println("----");
				System.out.println("Blog id = " + blogs[i].getBlogid());
				System.out.println("Blog name = " + blogs[i].getBlogName());
				System.out.println("Blog URL = " + blogs[i].getUrl());
			}
			
////			Integer postnum = blogger.newPost(api_key, "1", name, password, "this is a test<br/>foo.", false);
////			
////			System.out.println("i = " + postnum.toString());
//			
////			Map content = new HashMap();
////			Vector categories = new Vector();
////			categories.add("Auburn");
////			
////			content.put("title", "This is a test!");
////			content.put("description", "this is another test<br/>bar.");
////			content.put("categories", categories);
////			
////			String pnum2 = mweblog.newPost("1", name, password, content, false);
////			
////			System.out.println("j = " + pnum2);
//			
////			Map m = mweblog.getPost("768", name, password);
////			Set<Map.Entry<String, Object>> mset;
////			mset = m.entrySet();
////			Iterator<Map.Entry<String, Object>> iter = mset.iterator();
////			while (iter.hasNext())
////			{
////				Map.Entry<String, Object> entry = iter.next();
////				System.out.println("name = " + entry.getKey() + ", <" + entry.getValue() + ">");
////			}
//			
//			Object posts[] = mweblog.getRecentPosts("1", l_blog_username, l_blog_password, "4");
//			for (int i = 0; i < posts.length; i++) {
//				Map post = (Map) posts[i];
//				Set<Map.Entry<String, Object>> mset;
//				
//				mset = post.entrySet();
//				Iterator<Map.Entry<String, Object>> iter = mset.iterator();
//				
//				System.out.println("Post " + i);
//				while (iter.hasNext())
//				{
//					Map.Entry<String, Object> entry = iter.next();
//					System.out.println("  name = " + entry.getKey() + ", <" + entry.getValue() + ">");
//				}
//			}
		}
		catch (MalformedURLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (Fault e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}	
			
	private String 		b_url;
	private String 		b_user;
	private String 		b_password;
	private String 		b_blog_id;
	
	@SuppressWarnings("unused")
	private Blogger		b_blogger;
	private MetaWeblog	b_meta_weblog;
}
